﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

namespace LabMS.Web.Equipment.common
{
    public partial class reappedit : Base.BasePage
    {
        public string strID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            strPageUserType.Add("teacher");

            if (Request.QueryString["id"] == null)
            {
                Common.JShelper.JSAlert(Page, "err", "参数错误!");
               
                return;
            }
            if (Request.QueryString["Type"] != null)
            {
                tbEType.Text = Request.QueryString["Type"].ToString();
            }
            strID = Request.QueryString["id"].ToString();
            if (!Page.IsPostBack)
            {
                ddlLabs.Attributes.Add("onchange", "LabChange()");
                ddlEType.Attributes.Add("onchange", "LabChange()");
                tbRecipientsDate.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this);");
                tbEType.Style.Add("display", "none");
                tbCNub.Attributes.Add("readOnly", "-1");
                tbRecipientsDate.Attributes.Add("readOnly", "-1");
                lbCName.Attributes.Add("readOnly", "-1");
                lbTypeNub.Attributes.Add("readOnly", "-1");
                lbAllNub.Attributes.Add("readOnly", "-1");
                tbLabName.Attributes.Add("readOnly", "-1");

                tbEType.Style.Add("display", "none");
                tbID.Style.Add("display", "none");
                LabMS.Model.RecipientsApp mRecipientsApp = checkQueryString(strID);
                if (mRecipientsApp == null)
                {
                    Common.JShelper.JSAlert(Page, "err2", "借出申请不存在!");
                    
                    return;
                }

                bind(mRecipientsApp);
                bindDDL(mRecipientsApp);
                tbID.Text = mRecipientsApp.ID.ToString();
            }

        }

       

        LabMS.Model.RecipientsApp checkQueryString(string strID)
        {
            LabMS.BLL.RecipientsApp bRecipientsApp = new LabMS.BLL.RecipientsApp();
            string strWhere = "ID =" + strID;
            List<LabMS.Model.RecipientsApp> listmRecipientsApp = bRecipientsApp.GetModelList(strWhere);
            if (listmRecipientsApp.Count == 0)
            {
                return null;
            }
            return listmRecipientsApp[0];
        }

        public void bindDDL(LabMS.Model.RecipientsApp mRecipientsApp)
        {
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList(" Lab_ParentID is null ");
            ddlLabs.DataSource = mLab;
            ddlLabs.DataTextField = "Lab_Name";
            ddlLabs.DataValueField = "Lab_Code";
            ddlLabs.DataBind();
            ddlLabs.SelectedValue = mRecipientsApp.EncodedWarehouse;

            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();

            listCClass = eDictionary.GetItemListByDClass("shiyongfangxiang");
            ddlArrow.DataSource = listCClass;
            ddlArrow.DataTextField = "Name";
            ddlArrow.DataValueField = "Code";
            ddlArrow.DataBind();
            ddlArrow.SelectedValue = mRecipientsApp.Arrow;

            ddlEType.SelectedValue = mRecipientsApp.Type;

        }

        void bind(LabMS.Model.RecipientsApp recipientsApp)
        {

            Comment.Text = Common.codehelper.Decode(recipientsApp.AppComment);

            tbUserNub.Text = recipientsApp.AppNub;
            tbCNub.Text = recipientsApp.ClassNumber;
            List<LabMS.Model.InstrumentAndConsumables> mconsum = new List<LabMS.Model.InstrumentAndConsumables>();
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();

            mconsum = bconsum.GetModelList("Numbers='" + recipientsApp.ClassNumber + "'");
            if (mconsum.Count == 0)
            {
                Common.JShelper.JSAlert(Page, "err", "数据错误，请删除此条记录!");
                return;
            }
            try
            {
                lbAllNub.Text = mconsum[0].Quantity.Value.ToString();
            }
            catch
            {
            }
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            mLab = blab.GetModelList("Lab_Code='" + recipientsApp.Laboratory + "'");
            if (mLab.Count > 0)
            {
                tbLabName.Text = mLab[0].Lab_Name;
            }
            tbLabCode.Value = recipientsApp.Laboratory;

            lbCName.Text = recipientsApp.Name;
            try
            {
                tbRecipientsDate.Text = recipientsApp.RecipientTime.Value.ToShortDateString();
            }
            catch { }


            lbTypeNub.Text = recipientsApp.Words;

        }

        protected void Submit_Click(object sender, EventArgs e)
        {

            #region 验证
            if (string.IsNullOrEmpty(tbCNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "耗材编号不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbUserNub.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用数量不能为空!");
                return;
            }
            string strTotalNub = lbAllNub.Text.Trim();
            decimal iTotalNub = 0;
            decimal.TryParse(strTotalNub, out iTotalNub);

            string strUseNub = tbUserNub.Text.Trim();
            decimal iUserNub = 0;
            decimal.TryParse(strUseNub, out iUserNub);

            if (iUserNub > iTotalNub)
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "库存不足!");
                return;
            }

            if (string.IsNullOrEmpty(tbLabName.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用实验室不能为空!");
                return;
            }
            if (string.IsNullOrEmpty(tbRecipientsDate.Text.Trim()))
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用时间不能为空!");
                return;
            }

            #endregion

            lbErr.Visible = false;
            Button btn = (Button)sender;
            string strbtnName = btn.ID;
            LabMS.BLL.RecipientsApp brecipientsApp = new LabMS.BLL.RecipientsApp();
            LabMS.Model.RecipientsApp recipientsApp = new LabMS.Model.RecipientsApp();
            recipientsApp = brecipientsApp.GetModel(long.Parse(tbID.Text));
            if (recipientsApp == null)
            {
                Common.JShelper.JSAlert(Page, "err", "数据错误，请删除此条记录!");
                return;
            }
            recipientsApp.State = "0";
            if (strbtnName == "Submit")
            {
                recipientsApp.State = "1";
                recipientsApp.AppTime = DateTime.Now;
            }
            recipientsApp.AppComment = Common.codehelper.Encode(Comment.Text.Trim());
            recipientsApp.AppNub = tbUserNub.Text.Trim();
            recipientsApp.Arrow = ddlArrow.SelectedValue;
            recipientsApp.ClassNumber = tbCNub.Text;
            recipientsApp.EncodedWarehouse = ddlLabs.SelectedValue;
            recipientsApp.Laboratory = tbLabCode.Value;
            recipientsApp.MRecipients = "";//权限加上
            recipientsApp.Name = lbCName.Text;
            recipientsApp.Numbers = "";//编号不需要
            DateTime temp = new DateTime();
            if (DateTime.TryParse(tbRecipientsDate.Text, out temp))
            {
                recipientsApp.RecipientTime = temp;
            }
            else
            {
                LabMS.Common.JShelper.JSAlert(Page, "err", "领用时间格式错误!");
                return;
            }
            recipientsApp.Type = ddlEType.SelectedValue;
            recipientsApp.Words = lbTypeNub.Text;
            try
            {
                brecipientsApp.Update(recipientsApp);
                if (tbEType.Text != "")
                {
                    LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "操作成功!", "recipientappperview.aspx?ID=" + tbID.Text + "&Type=" + tbEType.Text);
                }
                else
                {
                    LabMS.Common.JShelper.JSAlertAndRedirect(Page, "AddSuccess", "操作成功!", "recipientappperview.aspx?ID=" + tbID.Text);
                }
            }
            catch (Exception ex)
            {
                lbErr.Text = ex.Message;
                lbErr.Visible = true;
            }
        }

    }
}
